const { searchTag, searchTagTotal, insertTag, updateTag, deleteTag } = require('../service/tag.service');

class TagController {
  // 查询标签
  async search_tag(ctx) {
    // 1、接收参数
    const { pageNum, pageSize } = ctx.request.body;
    try {
      const res = await searchTag({pageNum, pageSize});
      // 查询总数
      const resTotal = await searchTagTotal();
      let total = resTotal[0].total;

       // 3、返回数据
       ctx.body = {
        code: 200,
        data: {
          result: res,
          total: total
        },
        msg: "查询成功",
      }
    } catch(err) {
      console.log(err)
    }
  }

  // 新增标签
  async add_tag(ctx) {
    // 1、接收参数
    const {tag_name} = ctx.request.body;

    try {
      const res = await insertTag({tag_name});
       // 3、返回数据
      ctx.body = {
        code: 200,
        data: {},
        msg: "新增标签成功",
      }
    } catch(err) {
      console.log('新增标签报错====',err)
    }
  }
  // 修改标签
  async edit_tag(ctx) {
    // 1、接收参数
    const {id, tag_name} = ctx.request.body;

    try {
      const res = await updateTag({id, tag_name});
       // 3、返回数据
      ctx.body = {
        code: 200,
        data: {},
        msg: "修改标签成功",
      }
    } catch(err) {
      console.log(err)
    }
  }

   // 删除菜单
   async del_tag(ctx) {
    // 1、接收参数
    const {id} = ctx.request.body;
    try {

      const res = await deleteTag({id});

       // 3、返回数据
       ctx.body = {
        code: 200,
        data: {},
        msg: "删除标签成功",
      }
    } catch(err) {
      console.log(err)
    }
  }

}

module.exports = new TagController()